home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / zgstate.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  287 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zgstate.c */
  20. /* Graphics state operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "alloc.h"
  25. #include "gsmatrix.h"
  26. #include "gsstate.h"
  27. #include "state.h"
  28. #include "store.h"
  29.  
  30. /* Forward references */
  31. private int near num_param(P2(os_ptr, int (*)(P2(gs_state *, floatp))));
  32. private int near line_param(P2(os_ptr, int *));
  33.  
  34. /* ------ Operations on the entire graphics state ------ */
  35.  
  36. /* The current graphics state */
  37. gs_state *igs;
  38. int_gstate istate;
  39.  
  40. /* "Client" procedures */
  41. private char/*void*/ *gs_istate_alloc(P1(const gs_memory_procs *mp));
  42. private int gs_istate_copy(P2(char/*void*/ *to, const char/*void*/ *from));
  43. private void gs_istate_free(P2(char/*void*/ *old, const gs_memory_procs *mp));
  44. private const gs_state_client_procs istate_procs = {
  45.     gs_istate_alloc,
  46.     gs_istate_copy,
  47.     gs_istate_free
  48. };
  49.  
  50. /* Initialize the graphics stack. */
  51. void
  52. igs_init(void)
  53. {    igs = gs_state_alloc(&alloc_memory_procs);
  54.     int_gstate_map_refs(&istate, make_null);
  55.     gs_state_set_client(igs, (char/*void*/ *)&istate, &istate_procs);
  56.     /* gsave and grestore only work properly */
  57.     /* if there are always at least 2 entries on the stack. */
  58.     /* We count on the PostScript initialization code to do a gsave. */
  59. }
  60.  
  61. /* - gsave - */
  62. int
  63. zgsave(register os_ptr op)
  64. {    return gs_gsave(igs);
  65. }
  66.  
  67. /* - grestore - */
  68. int
  69. zgrestore(register os_ptr op)
  70. {    return gs_grestore(igs);
  71. }
  72.  
  73. /* - grestoreall - */
  74. int
  75. zgrestoreall(register os_ptr op)
  76. {    return gs_grestoreall(igs);
  77. }
  78.  
  79. /* - initgraphics - */
  80. int
  81. zinitgraphics(register os_ptr op)
  82. {    return gs_initgraphics(igs);
  83. }
  84.  
  85. /* ------ Operations on graphics state elements ------ */
  86.  
  87. /* <num> setlinewidth - */
  88. int
  89. zsetlinewidth(register os_ptr op)
  90. {    return num_param(op, gs_setlinewidth);
  91. }
  92.  
  93. /* - currentlinewidth <num> */
  94. int
  95. zcurrentlinewidth(register os_ptr op)
  96. {    push(1);
  97.     make_real(op, gs_currentlinewidth(igs));
  98.     return 0;
  99. }
  100.  
  101. /* <cap_int> setlinecap - */
  102. int
  103. zsetlinecap(register os_ptr op)
  104. {    int param;
  105.     int code = line_param(op, ¶m);
  106.     if ( !code ) code = gs_setlinecap(igs, (gs_line_cap)param);
  107.     return code;
  108. }
  109.  
  110. /* - currentlinecap <cap_int> */
  111. int
  112. zcurrentlinecap(register os_ptr op)
  113. {    push(1);
  114.     make_int(op, (int)gs_currentlinecap(igs));
  115.     return 0;
  116. }
  117.  
  118. /* <join_int> setlinejoin - */
  119. int
  120. zsetlinejoin(register os_ptr op)
  121. {    int param;
  122.     int code = line_param(op, ¶m);
  123.     if ( !code ) code = gs_setlinejoin(igs, (gs_line_join)param);
  124.     return code;
  125. }
  126.  
  127. /* - currentlinejoin <join_int> */
  128. int
  129. zcurrentlinejoin(register os_ptr op)
  130. {    push(1);
  131.     make_int(op, (int)gs_currentlinejoin(igs));
  132.     return 0;
  133. }
  134.  
  135. /* <num> setmiterlimit - */
  136. int
  137. zsetmiterlimit(register os_ptr op)
  138. {    return num_param(op, gs_setmiterlimit);
  139. }
  140.  
  141. /* - currentmiterlimit <num> */
  142. int
  143. zcurrentmiterlimit(register os_ptr op)
  144. {    push(1);
  145.     make_real(op, gs_currentmiterlimit(igs));
  146.     return 0;
  147. }
  148.  
  149. /* <array> <offset> setdash - */
  150. int
  151. zsetdash(register os_ptr op)
  152. {    float offset;
  153.     uint n, i;
  154.     const ref *dfrom;
  155.     float *pattern, *dto;
  156.     int code = real_param(op, &offset);
  157.     if ( code ) return code;
  158.     check_array(op[-1]);
  159.     check_read(op[-1]);
  160.     /* Unpack the dash pattern and check it */
  161.     dfrom = op[-1].value.const_refs;
  162.     i = n = r_size(op - 1);
  163.     pattern = dto = (float *)alloc(n, sizeof(float), "setdash");
  164.     while ( i-- )
  165.        {    switch ( r_type(dfrom) )
  166.            {
  167.         case t_integer:
  168.             *dto++ = dfrom->value.intval;
  169.             break;
  170.         case t_real:
  171.             *dto++ = dfrom->value.realval;
  172.             break;
  173.         default:
  174.             alloc_free((char *)dto, n, sizeof(float), "setdash");
  175.             return_error(e_typecheck);
  176.            }
  177.         dfrom++;
  178.        }
  179.     code = gs_setdash(igs, pattern, n, offset);
  180.     if ( !code ) pop(2);
  181.     return code;
  182. }
  183.  
  184. /* - currentdash <array> <offset> */
  185. int
  186. zcurrentdash(register os_ptr op)
  187. {    int n = gs_currentdash_length(igs);
  188.     int i = n;
  189.     ref parr;
  190.     int code = alloc_array(&parr, a_all, n, "currentdash");
  191.     ref *pattern = parr.value.refs;
  192.     ref *dto = pattern;
  193.     float *dfrom = (float *)((char *)pattern + n * (sizeof(ref) - sizeof(float)));
  194.     if ( code < 0 ) return code;
  195.     gs_currentdash_pattern(igs, dfrom);
  196.     while ( i-- )
  197.        {    make_real_new(dto, *dfrom);
  198.         dto++, dfrom++;
  199.        }
  200.     push(2);
  201.     ref_assign(op - 1, &parr);
  202.     make_real(op, gs_currentdash_offset(igs));
  203.     return 0;
  204. }
  205.  
  206. /* <num> setflat - */
  207. int
  208. zsetflat(register os_ptr op)
  209. {    return num_param(op, gs_setflat);
  210. }
  211.  
  212. /* - currentflat <num> */
  213. int
  214. zcurrentflat(register os_ptr op)
  215. {    push(1);
  216.     make_real(op, gs_currentflat(igs));
  217.     return 0;
  218. }
  219.  
  220. /* ------ Initialization procedure ------ */
  221.  
  222. op_def zgstate_op_defs[] = {
  223.     {"0currentdash", zcurrentdash},
  224.     {"0currentflat", zcurrentflat},
  225.     {"0currentlinecap", zcurrentlinecap},
  226.     {"0currentlinejoin", zcurrentlinejoin},
  227.     {"0currentlinewidth", zcurrentlinewidth},
  228.     {"0currentmiterlimit", zcurrentmiterlimit},
  229.     {"0grestore", zgrestore},
  230.     {"0grestoreall", zgrestoreall},
  231.     {"0gsave", zgsave},
  232.     {"0initgraphics", zinitgraphics},
  233.     {"2setdash", zsetdash},
  234.     {"1setflat", zsetflat},
  235.     {"1setlinecap", zsetlinecap},
  236.     {"1setlinejoin", zsetlinejoin},
  237.     {"1setlinewidth", zsetlinewidth},
  238.     {"1setmiterlimit", zsetmiterlimit},
  239.     op_def_end(0)
  240. };
  241.  
  242. /* ------ Internal routines ------ */
  243.  
  244. /* Allocate the interpreter's part of a graphics state. */
  245. private char/*void*/ *
  246. gs_istate_alloc(const gs_memory_procs *mp)
  247. {    ref arr;
  248.     int code = alloc_array(&arr, 0, sizeof(int_gstate) / sizeof(ref), "int_gsave");
  249.     return (code < 0 ? 0 : (char *)arr.value.refs);
  250. }
  251.  
  252. /* Copy the interpreter's part of a graphics state. */
  253. private int
  254. gs_istate_copy(char/*void*/ *to, const char/*void*/ *from)
  255. {    *(int_gstate *)to = *(int_gstate *)from;
  256.     return 0;
  257. }
  258.  
  259. /* Free the interpreter's part of a graphics state. */
  260. private void
  261. gs_istate_free(char/*void*/ *old, const gs_memory_procs *mp)
  262. {    ref arr;
  263.     make_array(&arr, 0, sizeof(int_gstate) / sizeof(ref), (ref *)old);
  264.     alloc_free_array(&arr, "int_grestore");
  265. }
  266.  
  267. /* Get a numeric parameter */
  268. private int near
  269. num_param(os_ptr op, int (*pproc)(P2(gs_state *, floatp)))
  270. {    float param;
  271.     int code = real_param(op, ¶m);
  272.     if ( !code ) code = (*pproc)(igs, param);
  273.     if ( !code ) pop(1);
  274.     return code;
  275. }
  276.  
  277. /* Get an integer parameter 0-2. */
  278. private int near
  279. line_param(register os_ptr op, int *pparam)
  280. {    check_type(*op, t_integer);
  281.     if ( op->value.intval < 0 || op->value.intval > 2 )
  282.         return_error(e_rangecheck);
  283.     *pparam = (int)op->value.intval;
  284.     pop(1);
  285.     return 0;
  286. }
  287.